//
//  UIButton+CNExtension.m
//  CsdnPlus
//
//  Created by wxlan on 2019/1/11.
//  Copyright © 2019年 CSDN. All rights reserved.
//

#import "UIButton+CNExtension.h"
#define angleValue(angle) ((angle) * M_PI / 180.0)//角度数值转换宏

@implementation UIButton (CNExtension)

- (void)addDiggButtonAnimationWihtLineColor:(UIColor *)lineColor isRote:(BOOL)isRote{
    

    self.userInteractionEnabled = NO;
    // 按钮图片动画
    CAShapeLayer *shapeLayer1 = [self drawLightInView:self color:lineColor atIndex:-1];
    CAShapeLayer *shapeLayer2 = [self drawLightInView:self color:lineColor atIndex:0];
    CAShapeLayer *shapeLayer3 = [self drawLightInView:self color:lineColor atIndex:1];

    [self.layer addSublayer:shapeLayer1];
    [self.layer addSublayer:shapeLayer2];
    [self.layer addSublayer:shapeLayer3];

    NSArray *tempShapeLayerArray = @[shapeLayer1, shapeLayer2, shapeLayer3];
    CGFloat lineAnimationDuration = 0.5;
    for (CAShapeLayer *shapeLayer in tempShapeLayerArray) {
        
        CABasicAnimation *lineAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
        lineAnimation.fromValue = @(0);
        lineAnimation.toValue = @(1);
        lineAnimation.removedOnCompletion = NO;
        lineAnimation.fillMode = kCAFillModeForwards;
        lineAnimation.duration = lineAnimationDuration;
        lineAnimation.repeatCount = 1;
        [shapeLayer addAnimation:lineAnimation forKey:@"lineAnimation"];
       
    }

    [UIView animateWithDuration:0.2 animations:^{
        // 旋转动画
        CAKeyframeAnimation *rotateAnimation = [CAKeyframeAnimation animation];
        rotateAnimation.keyPath = @"transform.rotation";
        rotateAnimation.values = @[@(angleValue(-20)),@(angleValue(5)),@(angleValue(0))];
        rotateAnimation.duration = 0.2;
        rotateAnimation.repeatCount = 1;
        
        
        
        // 缩放动画
        CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.values = @[@(1),@(1.4),@(1)];
        rotateAnimation.duration = 0.2;
        rotateAnimation.repeatCount = 1;
        
        
        
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.duration = 0.2;
        group.animations = @[rotateAnimation, scaleAnimation];
        [self.imageView.layer addAnimation:group forKey:@"imageAnimation"];
    } completion:^(BOOL finished) {
        if (isRote) {
            self.transform = CGAffineTransformRotate(self.transform, angleValue(-5));
        }
    }];
    
    
    if(self.currentBackgroundImage){

        UIImage *image = self.currentBackgroundImage;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:self.currentBackgroundImage];
        [self insertSubview:imageView atIndex:0];
        [self setBackgroundImage:nil forState:UIControlStateNormal];
        
        CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
        scaleAnimation.values = @[@(0),@(1.2),@(1)];
        scaleAnimation.duration = 0.4;
        [imageView.layer addAnimation:scaleAnimation forKey:@"backgroundImageAnimation"];
 
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(scaleAnimation.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self setBackgroundImage:image forState:UIControlStateNormal];
            [imageView removeFromSuperview];
            
        });

    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(lineAnimationDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.userInteractionEnabled = YES;
        
        for (CAShapeLayer *shapeLayer in tempShapeLayerArray) {
            
            [shapeLayer removeFromSuperlayer];
            
        }
        

    });
    
}
- (CAShapeLayer  *)drawLightInView:(UIView *)view color:(UIColor *)color atIndex:(NSInteger)index{
    CGFloat centerX = CGRectGetWidth(view.bounds)*0.5;
    CGFloat centerY = CGRectGetHeight(view.bounds)*0.5;
    
    CGPoint center = CGPointMake(centerX, centerY);
    CGFloat radius = 20;
    
    CAShapeLayer* shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor = color.CGColor;
    shapeLayer.path = ({
        UIBezierPath *path = [UIBezierPath bezierPath];
        
        CGFloat minAngle = (2*M_PI/12.0);
        CGFloat angle = minAngle;
//        for (int i = 1; i < 4; i++) {
        
            angle = minAngle * index;
            
            DDLogDebug(@"%@",@(angle));
            // 半径为 radius*0.7 的圆上的点
            CGFloat rate = 1-5/radius;
            CGFloat midx = center.x - sinf(angle)*radius*rate;
            CGFloat midy = center.y - cosf(angle)*radius*rate;
            
            // 半径为 radius 的圆上的点
            CGFloat maxx = center.x - sinf(angle)*radius;
            CGFloat maxy = center.y - cosf(angle)*radius;
            
            [path moveToPoint:CGPointMake(midx, midy)];
            [path addLineToPoint:CGPointMake(maxx, maxy)];
//        }
        
        path.CGPath;
    });
    shapeLayer.lineWidth = 1.5f;
    shapeLayer.lineJoin = kCALineJoinRound;
    shapeLayer.lineCap = kCALineCapRound;
    return shapeLayer;
}

@end
